<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '../../dashboard/admin/components/mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    },
    titleName: {
      type: String,
      default: '计划达成率趋势图'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
      console.log(this.chartData)
    },
    setOptions({ timeList, bar1data, bar2data, lineData, bar1name, bar2name, linename } = {}) {
      console.log(timeList, bar1data, bar2data, lineData, bar1name, bar2name, linename)
      this.chart.setOption({
        title: {
          left: 'center',
          text: this.titleName
        },
        xAxis: [
          {
            type: 'category',
            data: timeList,
            axisPointer: {
              type: 'shadow'
            }
          }
        ],
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: [
          {
            type: 'value',
            axisLabel: {
              formatter: '{value}'
            }
          },
          {
            type: 'value',
            axisLabel: {
              formatter: '{value} %'
            }
          }
        ],
        series: [{
          name: bar1name, itemStyle: {
            normal: {
              color: '#99CC33',
              lineStyle: {
                color: '#99CC33',
                width: 2
              }
            }
          },
          smooth: true,
          type: 'bar',
          data: bar1data,
          animationDuration: 2000,
          animationEasing: 'cubicInOut'
        },
        {
          name: bar2name, itemStyle: {
            normal: {
              color: '#3399CC',
              lineStyle: {
                color: '#3399CC',
                width: 2
              }
            }
          },
          smooth: true,
          type: 'bar',
          data: bar2data,
          animationDuration: 2000,
          animationEasing: 'cubicInOut'
        },
        {
          name: linename,
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#E9967A',
              lineStyle: {
                color: '',
                width: 2
              },
              areaStyle: {
                color: '#E9967A'
              }
            }
          },
          data: lineData,
          animationDuration: 2000,
          animationEasing: 'quadraticOut'
        }]
      })
    }
  }
}
</script>
